//
//  HomeTransferOrderInfoViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/3.
//

#import "HomeTransferOrderInfoViewController.h"
#import "HomeTransforOrderNetWorking.h"

#import "HomeTransforOrderInfoSecondOneTableViewCell.h"
#import "HomeTransforOrderInfoSecondTwoTableViewCell.h"
#import "HomeTransforOrderInfoFootView.h"

#import "HomeTransforOrderInfoModel.h"


@interface HomeTransferOrderInfoViewController ()<UITableViewDelegate,UITableViewDataSource,protocolUploadPhotoDelegate,protocolChooseToolsDelegate>

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) HomeTransforOrderInfoNetWorking * manager;
@property (nonatomic,strong) HomeTransforOrderInfoModel * model;
@property (nonatomic,strong) HomeTransforOrderInfoFootView * footView;




@property (nonatomic,strong) HomeAppGoodsTransferNetWorking * goodsManager;
@property (nonatomic,strong) NSMutableArray * photoArr;
@property (nonatomic,strong) NSMutableArray * GoodsArr;
@property (nonatomic,copy) NSString * ToUserStr;
@property (nonatomic,copy) NSString * CcUserStr;
@property (nonatomic,copy) NSString * inputNoteStr;
@property (nonatomic,copy) NSString * ChooseIsType;



@end

@implementation HomeTransferOrderInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"转单详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    [self loadNet];
    [self setUpUI];
    
    
    
}

-(void)setUpUI{
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.transferOrderBtn];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-ALIGN_HEIGHT_PT(100));
    }];
    
    [self.transferOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(12));
        make.bottom.equalTo(self.view).with.offset(-ALIGN_HEIGHT_PT(30));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
    }];
}



#pragma mark -- 网络请求
-(void)loadNet{
    
    [self.manager dispatchTaskWithParam:@{@"sourceId":self.ID} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            
            self.model = [HomeTransforOrderInfoModel mj_objectWithKeyValues:responseObject[@"data"]];
            [self.dataSource addObject:self.model]; // 基本信息
            if (self.model.inList.count > 0){
                [self.dataSource addObject:self.model]; // 工具货物信息
            }
            if (self.model.outList.count > 0){
                [self.dataSource addObject:self.model]; // 工具货物信息
            }
            
            [self.dataSource addObject:self.model];
            
            
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
            return;
        }
    }];
    
    
}


#pragma mark -- 点击事件
-(void)respondsto_transferOrderBtn{
    
    if (self.ToUserStr.length <= 0){
        [MBProgressHUD showError:@"请选择接收人"];
        return;
    }else if (self.CcUserStr.length <= 0){
        [MBProgressHUD showError:@"请选择抄送人"];
        return;
    }else if (self.inputNoteStr.length <= 0){
        [MBProgressHUD showError:@"请输入转单说明"];
        return;
    }else if (self.GoodsArr.count == 0){
        [MBProgressHUD showError:@"请选择带入/带出明细"];
        return;
    }else{
        
        NSDictionary * paramer = @{@"toUser":self.ToUserStr,
                                   @"ccUser":@[self.CcUserStr],
                                   @"remark":self.inputNoteStr,
                                   @"photo":self.photoArr,
                                   @"goodsTransferDetailVOList":self.GoodsArr,
                                   @"type":self.ChooseIsType
        };
        
        NSError *error;
                NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paramer
                                                                   options:NSJSONWritingPrettyPrinted
                                                                     error:&error];
        NSString *jsonString;
                if (!jsonData) {
                    NSLog(@"Error: %@", error);
                } else {
                    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                    NSLog(@"JSON String: %@", jsonString);
                }
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = @"加载中...";
        
        [self.goodsManager dispatchTaskWithParam:paramer completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            [hud hideAnimated:YES];
            
            if (responseObject && [responseObject[@"code"] intValue] == 200){
                [MBProgressHUD showSuccess:@"转单成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                    return;
                }];
            }
        }];
        
        
        
        
        
        
        
    }
    
    
}


#pragma mark -- protocolUploadPhotoDelegate
-(void)chooseUploadArr:(NSArray *)arr{
    NSLog(@"sssss ==  %@",arr);
    self.photoArr = [NSMutableArray arrayWithArray:arr];
}

-(void)chooseReceiver:(NSString *)ReceiverStr{
    self.ToUserStr = ReceiverStr;
}

-(void)chooseCS:(NSString *)CSStr{
    self.CcUserStr = CSStr;
}
-(void)inputNote:(NSString *)noteStr{
    self.inputNoteStr = noteStr;
}


#pragma mark -- protocolChooseToolsDelegate
-(void)chooseToolDataSource:(NSArray *)arr IsType:(NSInteger)istype{
    self.ChooseIsType = [NSString stringWithFormat:@"%@",@(istype)];
    self.GoodsArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(HomeTransforOrderInfoAreaListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary * dic = @{@"goodsId":obj.goodsId,
                               @"goodsName":obj.goodsName,
                               @"goodsSpec":obj.goodsSpec,
                               @"goodsNum":obj.goodsNum,
                               @"goodsType":obj.goodsType,
                               @"sourceId":obj.id
        };
        [self.GoodsArr addObject:dic];
    }];
    
}





#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeTransforOrderInfoModel * model = self.dataSource[indexPath.row];
    
    if (indexPath.row == 0){
        HomeTransforOrderInfoSecondOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransforOrderInfoSecondOneTableViewCell"];
        if (!cell){
            cell = [[HomeTransforOrderInfoSecondOneTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTransforOrderInfoSecondOneTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        
        return cell;
    }else if (indexPath.row == 1 && model.inList.count > 0){
        HomeTransforOrderInfoSecondTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransforOrderInfoSecondTwoTableViewCell"];
        if (!cell){
            cell = [[HomeTransforOrderInfoSecondTwoTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTransforOrderInfoSecondTwoTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.type = 1;
        cell.model = model;
        cell.delegate = self;
        cell.titleLabel.text = @"带入控制区明细";

        return cell;
    }else if (indexPath.row == 2 && model.outList.count > 0){
        HomeTransforOrderInfoSecondTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTransforOrderInfoSecondTwoTableViewCell"];
        if (!cell){
            cell = [[HomeTransforOrderInfoSecondTwoTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTransforOrderInfoSecondTwoTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.type = 2;
        cell.model = model;
        cell.delegate = self;
        cell.titleLabel.text = @"带出控制区明细";

        return cell;
    }
    
    return [UITableViewCell new];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    HomeTransforOrderInfoModel * model = self.dataSource[indexPath.row];

    if (indexPath.row == 0){
        return ALIGN_HEIGHT_PT(180);
    }else if (indexPath.row == 1 && model.inList.count > 0){
        
        NSInteger hei = ALIGN_HEIGHT_PT(118) + model.inList.count * ALIGN_HEIGHT_PT(32);
        return hei;
        
    }else if (indexPath.row == 2 && model.outList.count > 0){
        NSInteger hei = ALIGN_HEIGHT_PT(118) + model.outList.count * ALIGN_HEIGHT_PT(32);
        return hei;
    }
    return 0;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    self.footView = [[HomeTransforOrderInfoFootView alloc]init];
    self.footView.delegate = self;
    return self.footView;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [UIView new];
    return view;
}




-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return ALIGN_HEIGHT_PT(550);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}




#pragma mark -- 懒加载

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}


-(UIButton *)transferOrderBtn{
    if (!_transferOrderBtn){
        _transferOrderBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_transferOrderBtn setTitle:@"确认转单" forState:(UIControlStateNormal)];
        [_transferOrderBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _transferOrderBtn.titleLabel.font = FONT15;
        _transferOrderBtn.layer.cornerRadius = 8;
        _transferOrderBtn.layer.masksToBounds = YES;
        _transferOrderBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        [_transferOrderBtn addTarget:self action:@selector(respondsto_transferOrderBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _transferOrderBtn;
}



-(NSMutableArray *)dataSource{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(HomeTransforOrderInfoNetWorking *)manager{
    if (!_manager){
        _manager = [HomeTransforOrderInfoNetWorking new];
     }
    return _manager;
}

-(HomeAppGoodsTransferNetWorking *)goodsManager{
    if (!_goodsManager){
        _goodsManager = [HomeAppGoodsTransferNetWorking new];
    }
    return _goodsManager;
}


@end
